<?php
	require_once('functions.php');
	
	// PRODUCTS FUNCTIONS
	function GetProducts(){
		$conn = GetConnection();
		return $results = $conn -> query('SELECT * FROM WSP2012_Products');
	}
	
	function GetProduct($id){
		$conn= GetConnection();
		$sql = "SELECT * FROM WSP2012_Products WHERE products_id=$id ";
		$results = $conn -> query($sql);
		$row = $results -> fetch_assoc();
		$conn -> close();
		return $row;
	}
	
	function SaveProduct($row){
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = "UPDATE `WSP2012_Products` " 
			.	"Set category_id='$row2[category_id]', updated_at='$row2[updated_at]', name='$row2[name]', price='$row2[price]', "
			.	"description='$row2[description]', picture='$row2[picture]' " 	
			.	"WHERE products_id='$row2[id]' ";
		$results = $conn -> query($sql);
		$error = $conn -> error;
		$conn -> close();
		
		return $error == '' ? true : array('SQL Error' => $error);	
	}
	
	function CreateProduct($row){
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = "INSERT INTO `WSP2012_Products` "
			.		"(products_id, category_id, created_at, updated_at, name, price, description, picture) "
			.		"VALUES ('$row2[products_id]', '$row2[category_id]', '$row2[created_at]', '$row2[updated_at]', '$row2[name]', '$row2[price]', '$row2[description]', '$row2[picture]') ";
		$results = $conn -> query($sql);
		$error = $conn -> error;
		$conn -> close();	
		
		return $error == '' ? true : array('SQL Error' => $error);
	}
	
	function DeleteProduct($row, $id){
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = "DELETE from WSP2012_Products WHERE products_id= '$row2[id]' ";
		$results = $conn -> query($sql);
		$error = $conn -> error;
		$conn -> close();	
		
		return $error == '' ? true : array('SQL Error' => $error);
	}
?>
